<template>
  <n-card :bordered="false" class="proCard">
    <!-- <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
      <template #statusSlot="{ model, field }">
        <n-input v-model:value="model[field]" />
      </template>
    </BasicForm> -->

    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      ref="actionRef"
      :actionColumn="actionColumn"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
    >
      <template #tableTitle>
        <!-- <n-button type="primary" @click="addTable">
          <template #icon>
            <n-icon>
              <PlusOutlined />
            </n-icon>
          </template>
          新建
        </n-button> -->
      </template>

      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>
  </n-card>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { useDialog, useMessage } from 'naive-ui';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getPutSaleOrderPage, putSaleOrder } from '@/api/promotion/putSaleOrder';
  import { columns } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import { ResultEnum } from '@/enums/httpEnum';

  const rules = {
    orderSn: {
      required: true,
      trigger: ['blur', 'input'],
      message: '请输入订单号',
    },
    rushPurchaseId: {
      required: true,
      trigger: ['blur', 'input'],
      message: '请输入活动场次id',
    },
    // rushPurchaseDetailId: {
    //   required: true,
    //   trigger: ['blur', 'input'],
    //   message: '请输入抢购活动明细id',
    // },
    // nextRushPurchaseDetailId: {
    //   required: true,
    //   trigger: ['blur', 'input'],
    //   message: '请输入上架后，下一场抢购活动明细id',
    // },
    // originalPrice: {
    //   required: true,
    //   trigger: ['blur', 'input'],
    //   message: '请输入原价（购买价）',
    // },
    // newPrice: {
    //   required: true,
    //   trigger: ['blur', 'input'],
    //   message: '请输入上架后新价格',
    // },
    // servicePrice: {
    //   required: true,
    //   trigger: ['blur', 'input'],
    //   message: '请输入服务费（上架费）',
    // },
    // paymentOrderNo: {
    //   required: true,
    //   trigger: ['blur', 'input'],
    //   message: '请输入第三方支付单号',
    // },
    // memberId: {
    //   required: true,
    //   trigger: ['blur', 'input'],
    //   message: '请输入上架会员id',
    // },
    // status: {
    //   required: true,
    //   trigger: ['blur', 'input'],
    //   message: '请输入状态0默认1支付成功',
    // },
  };

  const schemas: FormSchema[] = [
    {
      field: 'orderSn',
      component: 'NInput',
      label: '订单号',
      rules: [{ required: false }],
    },
  ];

  const formRef: any = ref(null);
  const message = useMessage();
  const actionRef = ref();

  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const checkedPutSaleOrderId = ref(); //当前编辑id
  const isEdit = ref(false); // 弹出框是否为编辑
  const editTitle = ref('');
  const dialog = useDialog();
  const formParams = reactive({
    id: null,
    orderSn: '',
    rushPurchaseId: '',
    rushPurchaseDetailId: '',
    nextRushPurchaseDetailId: '',
    originalPrice: '',
    newPrice: '',
    servicePrice: '',
    paymentOrderNo: '',
    memberId: '',
    status: '',
  });
  const formParamsBak = { ...formParams };

  const params = ref({});

  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      if (record.status == 0) {
        return h(TableAction as any, {
          style: 'button',
          actions: [
            {
              label: '上架',
              onClick: handlePutSale.bind(null, record),
            },
            // {
            //   label: '删除',
            //   type: 'error',
            //   icon: 'ic:outline-delete-outline',
            //   onClick: handleDelete.bind(null, record),
            // },
          ],
        });
      }
    },
  });

  const [register, { }] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 100,
    schemas,
  });

  const loadDataTable = async (res) => {
    return await getPutSaleOrderPage({ ...params.value, ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function handlePutSale(record: Recordable) {
    console.log('点击了上架', record);
    dialog.info({
      title: '提示',
      content: `您确定想上架吗?`,
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        const params = { orderSn: record.orderSn };
        const response = await putSaleOrder(params);
        const { message: msg, code } = response;
        if (code == ResultEnum.SUCCESS) {
          message.success(msg);
          reloadTable();
        } else {
          message.error(msg);
        }
      },
      onNegativeClick: () => {
        message.error('已取消');
      },
    });
  }
</script>

<style lang="less" scoped></style>
